﻿Imports VSModel
Imports Cls_Common
Partial Class FrontEnd_Control_News_Video
    Inherits System.Web.UI.UserControl
    Dim iFirstSubMenu As Integer = 0
    Private strFileUrl As String
    Private strUrlYoutube As String
    Private strPlayerUrl As String
    'System.Configuration.ConfigurationManager.AppSettings("player_url")
    Private strVideoTitle As String

    Public Property UrlYoutube() As String
        Get
            Return strUrlYoutube
        End Get
        Set(ByVal Value As String)
            strUrlYoutube = Value
        End Set
    End Property
    Public Property FileUrl() As String
        Get
            Return strFileUrl
        End Get
        Set(ByVal Value As String)
            strFileUrl = Value
        End Set
    End Property
    Public Property PlayerUrl() As String
        Get
            Return strPlayerUrl
        End Get
        Set(ByVal Value As String)
            strPlayerUrl = Value
        End Set
    End Property
    Public Property VideoTitle() As String
        Get
            Return strVideoTitle
        End Get
        Set(ByVal Value As String)
            strVideoTitle = Value
        End Set
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            If Not Page.RouteData.Values("id") Is Nothing Then
                '  Dim iMenuId As Integer = Page.RouteData.Values("MenuId")
                Dim iId As Integer = Page.RouteData.Values("id")
                ShowDataDEtail(iId)
                'lbtnShared.Attributes.Add("onclick", "return SharedObject('" & iId & "')")

            End If
        End If
    End Sub

    'Show noi dung object
    Sub ShowDataDEtail(ByVal iobjId As Integer)
        Using data As New VSEntities
            Dim p = (From c In data.Contents Where c.Id = iobjId And c.StatusId = 3 Select c).FirstOrDefault
            If Not p Is Nothing Then
                If (Session("PageViewNews") Is Nothing) Then
                    Session("PageViewNews") = Now
                    'Update so luot xem len 1

                    If Not p.Viewed Is Nothing Then
                        p.Viewed = p.Viewed + 1
                    Else
                        p.Viewed = 1
                    End If

                    data.SaveChanges()
                Else
                    If (DateDiff(DateInterval.Second, CType(Session("PageViewNews"), Date), Now()) >= ConfigurationManager.AppSettings("SettingTimeUpdatePageView")) Then
                        'Update so luot xem len 1
                        If Not p.Viewed Is Nothing Then
                            p.Viewed = p.Viewed + 1
                        Else
                            p.Viewed = 1
                        End If
                        'p.Viewed = IIf(IsNothing(p.Viewed) = True, 1, p.Viewed + 1)
                        data.SaveChanges()
                        Session("PageViewNews") = Now
                    End If
                End If

                hplTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                'Dim _subjectid As Integer = (From q In data.ContentsSubjects Where q.ContentId = p.Id Select q.SubjectId).FirstOrDefault
                'ltrSubject.Text = (From q In data.Subjects Where q.SubjectId = _subjectid Select q.Subject1).FirstOrDefault
                ''If ltrSubject.Text = "" Then
                ''    ltrSubject.Text = "Chủ để bài viết"
                ''End If

                'Page.Title = hplTitle.Text & " - " & HeaderTitleSSM
                'Page.MetaDescription = p.Description
                'ltrDes.Text = p.Description
                ''Dim a = (From b In data.Questions Where b.StatusID = 3 And b.ExpertId = iobjId).Count

                'lblCountComment.Text = (From b In data.Questions Where b.StatusID = 3 And b.ExpertId = iobjId).Count
                'ltrContent.Text = IIf(IsNothing(p.Contents) = True, "", p.Contents)


                If p.IsYoutube = True Then
                    pnlVideoUpload.Visible = False
                    pnlYoutube.Visible = True
                    'VideoTitle() = p.Title
                    'FileUrl() = SplitVideoYoutube(p.URL)
                    ltrShow.Text = p.URL
                Else
                    Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
                    VideoTitle() = p.Title
                    PlayerUrl() = baseUrl + ResolveUrl_Video("~/jwplayer/player.swf")
                    FileUrl() = p.URL
                    pnlVideoUpload.Visible = True
                    pnlYoutube.Visible = False
                End If


            End If
        End Using
    End Sub

 



    Public Function ResolveUrl_Video(ByVal url As String) As String
        If url Is Nothing Then
            Throw New ArgumentNullException("url", "url can not be null")
        End If
        If url.Length = 0 Then
            Throw New ArgumentException("The url can not be an empty string", "url")
        End If

        ' there is no ~ in the first character position, just return the url 
        If url(0) <> "~"c Then
            Return url
        End If

        Dim applicationPath As String = HttpContext.Current.Request.ApplicationPath

        ' there is just the ~ in the URL, return the applicatonPath 
        If url.Length = 1 Then
            Return applicationPath
        End If

        ' assume url looks like ~somePage 
        Dim indexOfUrl As Integer = 1

        ' determine the middle character 
        Dim midPath As String = If((applicationPath.Length > 1), "/", String.Empty)

        ' if url looks like ~/ or ~\ change the indexOfUrl to 2 
        If url(1) = "/"c OrElse url(1) = "\"c Then
            indexOfUrl = 2
        End If

        Return applicationPath & midPath & url.Substring(indexOfUrl)
    End Function
    Protected Function getVideoYoutube(ByVal ID As String) As String
        Dim scr As String = "<object width='320' height='240'> "

        scr = scr & "<param name='movie' value='" + "http://www.youtube.com/v/" & ID & "'></param> "

        scr = scr & "<param name='allowFullScreen' value='true'></param> "

        scr = scr & "<param name='allowscriptaccess' value='always'></param> "

        scr = scr & "<embed src='" + "http://www.youtube.com/v/" & ID & "'"

        scr = scr & "type='application/x-shockwave-flash' allowscriptaccess='always' "

        scr = scr & "allowfullscreen='true' width='320' height='240'> "

        scr = scr & "</embed></object>"
        Return scr
    End Function
    Protected Function SplitVideoYoutube(ByVal strUrl As String) As String
        Dim intStart As Integer = strUrl.IndexOf("=")
        Dim intEnd As Integer = strUrl.IndexOf("&")
        Dim scr As String = strUrl.Substring(intStart + 1, strUrl.Length - intStart - 1)
        Return scr
    End Function


End Class
